package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcOtherService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 其他入库
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/other_in")
@RestController
public class XqcOtherInController {

    /** 其他服务 */
    @Autowired
    private IXqcOtherService otherService;

    /**
     * 保存其他出库单(到审核阶段)
     */
    @PostMapping("/save_user_other_in")
    @VtuzxApi
    public Object saveUserOtherIn(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        otherService.saveUserOtherIn( _userId, name, param);
        return null;
    }

    /**
     * 添加用户临时其他出库物料箱号
     */
    @PostMapping("/add_user_otherin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "数量", notNull = true, isNumber = true)
    } )
    public Object addUserOtherInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        double count = VtuzxUtil.getDouble(param, "count");
        return otherService.addUserOtherInMaterialBox(_userId, name, stockNo, locationNo, boxNo, count);
    }

    /**
     * 添加用户临时采购入库物料箱号
     */
    @PostMapping("/add_user_otherin_material_box_list")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "boxList", label = "箱号列表", notNull = true)
    } )
    public Object addUserOtherInMaterialBoxList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        return otherService.addUserOtherInMaterialBoxList(_userId, name, stockNo, locationNo, boxList);
    }

    /**
     * 删除用户临时其他出库物料箱号
     */
    @PostMapping("/delete_user_otherin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserOtherInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        return otherService.deleteUserOtherInMaterialBox(_userId, id);
    }

    /**
     * 删除用户临时其他出库物料
     */
    @PostMapping("/delete_user_otherin_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserOtherInMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        otherService.deleteUserOtherInMaterial(_userId, id);
        return null;
    }

    /**
     * 删除用户临时其他出库单(清空)
     */
    @PostMapping("/delete_user_otherin")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object deleteUserOtherIn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        otherService.deleteUserOtherIn(_userId, stockNo);
        return null;
    }

    /**
     * 编辑用户临时其他出库物料箱号
     */
    @PostMapping("/update_user_otherin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "materialNo", label = "物料编码", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "数量", notNull = true, isNumber = true)
    } )
    public Object updateUserOtherInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String materialNo = VtuzxUtil.getString(param, "materialNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        double count = VtuzxUtil.getDouble(param, "count");
        return otherService.updateUserOtherInMaterialBox(_userId, stockNo, locationNo, materialNo, boxNo, count);
    }

    /**
     * 获取用户临时其他出库物料列表
     */
    @PostMapping("/get_user_otherin_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object getUserOtherInMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        return otherService.getUserOtherInMaterial(_userId, stockNo);
    }

    /**
     * 获取用户临时其他出库物料箱号列表
     */
    @PostMapping("/get_user_otherin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "_detailId", label = "detailId", notNull = true),
    } )
    public Object getUserOtherInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String _detailId = VtuzxUtil.getString(param, "_detailId");
        return otherService.getUserOtherInMaterialBox(_userId, _detailId);
    }

    /**
     * 获取其他出库单
     */
    @PostMapping("/get_otherin_bill")
    @VtuzxApi
    public Object getOtherInBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, "pageIndex");
        int pageSize = VtuzxUtil.getInt(param, "pageSize");
        return otherService.getOtherInBill(_userId, param, pageIndex, pageSize);
    }

    /**
     * 获取其他入库单分录列表
     */
    @PostMapping("/get_otherin_bill_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object getOtherInBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return otherService.getOtherInBillDetail(_userId, stockNo, billNo);
    }

    /**
     * 获取其他入库单分录的箱号详细
     */
    @PostMapping("/get_otherin_bill_detail_box")
    @VtuzxApi
    public Object getOtherInBillDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String _detailId = VtuzxUtil.getString(param, "_detailId");
        return otherService.getOtherInBillDetailBox(_userId, _detailId);
    }

    /**
     * 修改其他入库单打印状态
     * @param token
     * @param param
     * @return
     * @throws VtuzxException
     */
    @PostMapping("/update_other_in_seal")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object updateOtherInSeal(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return otherService.updateOtherInSeal(_userId, name, param);
    }

    /**
     * 获取其他入库详情
     */
    @PostMapping("/download_otherIn_detail")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadInventory(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return otherService.downloadOtherInDetail(token, param);
    }
}
